package com.shahe.GirlsDay;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.shahe.GirlsDay.util.ContactPerson;
import com.shahe.GirlsDay.util.HttpHelper;
import com.shahe.GirlsDay.util.StaticClass;
import com.shahe.GirlsDay.util.UserInfo;

public class MainActivity extends Activity {
	/**
	 * Called when the activity is first created.
	 */

	private EditText usernameEdit;
	private EditText passwordEdit;
	private String username;
	private String password;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Button loginButton = (Button) findViewById(R.id.btn_login);
		final Button registerButton = (Button) findViewById(R.id.btn_register);
		usernameEdit = (EditText) findViewById(R.id.editText);
		passwordEdit = (EditText) findViewById(R.id.editText2);

		usernameEdit.setText("sarah001");
		passwordEdit.setText("890413");

		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				username = usernameEdit.getText().toString();
				password = passwordEdit.getText().toString();
				if (checkInput(username) && checkInput(password)) {

					String str = username + "," + password;
					String json = null;
					try {
						json = new myTask().execute(str).get();
					} catch (InterruptedException | ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (json.length() > 2) {
						StaticClass.myJson = json;
						// warring(json);
						try {
							JSONArray jsonArray = new JSONArray(
									StaticClass.myJson);
							if (jsonArray.length() != 0) {
								JSONObject jsonObject2 = jsonArray
										.getJSONObject(0);
								//warring(jsonObject2.toString());
								StaticClass.myInfo = new UserInfo(jsonObject2);
								//warring(StaticClass.myInfo.getEmail());
                                StaticClass.createOrChang = true;
								try {
									json = new getCPTask()
											.execute(
													StaticClass.myInfo
															.getUserid() + "")
											.get();
									if (json.length() > 2) {
										JSONArray jsonArray2 = new JSONArray(
												json);
										JSONObject jsonObject3 = jsonArray2
												.getJSONObject(0);
										StaticClass.cPersonInfo = new ContactPerson(
												jsonObject3);
									} else {
										warring("No contact person is in yourlist!");
									}
									//warring(StaticClass.cPersonInfo.getPhone());
									Log.i("123456", json);
								} catch (InterruptedException
										| ExecutionException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
	
								Intent intent = new Intent(MainActivity.this,
										HomeActivity.class);
								startActivity(intent);
								finish();

							} else {
								warring("Invalid Username or Password");
							}

						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					} else {
						warring("Invalid Username or Password");
					}

				}

			}
		});

		registerButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}
		});

	}

	public boolean checkInput(String str) {
		if (str.matches("^\\w+?")) {
			return true;
		} else if (str.equals(null)) {
			Toast.makeText(this, "null", Toast.LENGTH_LONG).show();
			return false;
		} else {
			warring("format error");
			return false;
		}
	}

	private class myTask extends AsyncTask<String, String, String> {

		protected String doInBackground(String... strings) {
			return HttpHelper.checkLogin(strings[0]);
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

	private class getCPTask extends AsyncTask<String, String, String> {

		protected String doInBackground(String... strings) {
			return HttpHelper.getCP(strings[0]);
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

	public void warring(String str) {
		Toast.makeText(this, str, Toast.LENGTH_LONG).show();
	}
}
